<?php

use yii\db\Migration;
use extend\Extend;
use common\models\Admin;
use builder\helper\T;

/**
 * Handles the creation of table `{{%admin}}`.
 */
class m230508_052850_create_admin_table extends Migration
{
    const TABLE_NAME = '{{%admin}}';

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB COMMENT="管理员表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey()->comment('ID'),
            'pid' => $this->integer()->notNull()->defaultValue(0)->comment('父ID'),
            'path' => $this->string()->notNull()->defaultValue('')->comment('路径, 例: -0-1-'),
            'invite_code' => $this->string()->notNull()->comment('邀请码'),
            'username' => $this->string(20)->unique()->notNull()->comment('用户名'),
            'password_hash' => $this->string(255)->notNull()->comment('密码'),
            'email' => $this->string(255)->notNull()->defaultValue('')->comment('邮箱'),
            'mobile' => $this->string(11)->notNull()->defaultValue('')->comment('手机号'),
            'photo' => $this->string()->defaultValue('')->notNull()->comment('头像'),
            'google_key' => $this->string(50)->defaultValue('')->notNull()->comment('MFA秘钥'),
            'auth_key' => $this->string()->defaultValue('')->notNull()->comment('COOKIE校验串'),
            'sso_key' => $this->string()->notNull()->comment('单点校验串'),
            'access_token' => $this->string()->notNull()->comment('访问令牌'),
            'status' => $this->boolean()->defaultValue(1)->notNull()->comment('状态: 0封停 1正常'),
            'deny_end_time' => $this->dateTime()->defaultValue(null)->comment('封停截止时间, Null:无期'),
            'group' => $this->integer()->notNull()->comment('管理组, 0:超管'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->defaultValue(null),
        ], $tableOptions);

        $this->createIndex('index_status', self::TABLE_NAME, ['status']);
        $this->createIndex('index_group', self::TABLE_NAME, ['group']);

        // 生成唯一的超级管理员账号
        $this->insert(self::TABLE_NAME, [
            'id' => 1,
            'pid' => 0,
            'path' => '-0-',
            'invite_code' => Admin::generateInviteCode(),
            'username' => 'yiimanager',
            'password_hash' => Yii::$app->security->generatePasswordHash('123456+++'),
            'email' => '',
            'mobile' => '',
            'photo' => '',
            'google_key' => Extend::googleAuth()->createSecret(),
            'auth_key' => '',
            'sso_key' => Admin::generateSSOKEY(),
            'access_token' => Admin::generateAccessToken(),
            'status' => Admin::STATUS_ACTIVE,
            'deny_end_time' => null,
            'group' => 0,
            'created_at' => T::now(),
            'updated_at' => T::now(),
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
